package basic;

import java.util.*;

public class ListDemo {
    private static final int size = 500;

    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        List<String> list = new ArrayList<>();
        for (int i = 0; i <= size; i++) {
            String item = itemOf(i);
            set.add(item);
            list.add(item);
        }
        for (int i = 0; i < 5; i++) {
            contains(set, "set" + i);
            contains(list, "list" + i);
        }
    }

    public static String itemOf(int i) {
        return i + i + "";
    }

    /**
     * 测试  set 和 list的 contains 方法效率
     * @param collection
     * @param mark
     */
    public static void contains(Collection<String> collection, String mark) {
        long start = System.currentTimeMillis();
        for (int i = size; i >= 0; i--) {
            collection.contains(itemOf(i));
        }
        System.out.printf("%s 耗时：%sms\n", mark, System.currentTimeMillis() - start);
    }

}
